/*
 * @version: vue3
 * @Author: lxs
 * @Date: 2022-04-30 14:05:30
 * @LastEditors: sueRimn
 * @LastEditTime: 2022-05-01 13:33:30
 */
import { login, LoginParm } from "@/api/user/user";
import { ElMessage } from "element-plus";
import { useStore } from "@/store";
import { useRouter } from "vue-router";
export default function useLogin(loginFormRef: any, loginModel: LoginParm) {
  const store = useStore();
  const router = useRouter();
  const Login = () => {
    loginFormRef.value.validate(async (valid: boolean) => {
      if (valid) {
        // console.log(store.state);
        // console.log(loginModel);
        try {
            const res = await store.dispatch("Login",loginModel);
            console.log(res);            
            router.push("/dashboard")
        } catch (error:any) {
          ElMessage.error(error.msg);
        }
        // try {
        //   let res: any = await login(loginModel);
        //   console.log(res);
        //   store.dispatch("Login", res);
        // } catch (error: any) {
        //   ElMessage.error(error.msg);
        // }
      } else {
        ElMessage.error("请先写账号密码");
      }
    });
  };

  return {
    Login,
  };
}
